//
// Copyright (c) 2014, 保留所有权利。
//
// FixContractHtTotal.java
//
// 作    者：王红燕
// 当前版本：1.0
// 2015 下午4:17:26
//
// 
//
package com.vanke.agent.fix;

import com.vanke.agent.ADataSource;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 */
public class FixBookingTotal {
	public static void main(String[] args) {
		Connection myCon = ADataSource.getConnection(ADataSource.SCHEMA_MY);
		Statement queryStmt = null;
		ResultSet queryRs = null;
		String querySql = "select Total,BookingGUID from s_Booking order by CreatedOn asc";

		Connection bizCon = ADataSource.getConnection(ADataSource.SCHEMA_BIZ);
		PreparedStatement updatePstmt = null;
		String updateSql = "update s_Booking set Total=? where BookingGUID=?";

		long now = System.currentTimeMillis();
		try {
			queryStmt = myCon.createStatement(
					ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			queryRs = queryStmt.executeQuery(querySql);

			updatePstmt = bizCon.prepareStatement(updateSql);
			int count = 0;
			while (queryRs.next()) {
				count++;
				updatePstmt.setDouble(1, queryRs.getDouble(1));
				updatePstmt.setString(2, queryRs.getString(2));
				updatePstmt.addBatch();
				if (count % 2000 == 0) {
					updatePstmt.executeBatch();
					updatePstmt.clearBatch();
					System.out.println(count + " booking's Total updated. "
							+ (System.currentTimeMillis() - now) + "ms");
				}
			} // end while
			updatePstmt.executeBatch();
			updatePstmt.clearBatch();
			System.out.println("all over. " + count + " booking's Total updated."
					+ (System.currentTimeMillis() - now) + "ms");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ADataSource.close(null, updatePstmt, bizCon);
			ADataSource.close(queryRs, queryStmt, myCon);
		}
	} //~main
} ///~
